package arithmetic_40;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author wxh
 */
public class Solution_6 {


	public int majorityElement(int[] nums) {

		// 1、将数组元素各自出现的次数统计到map中
		Map<Integer, Integer> MapCount = new HashMap<Integer, Integer>();
		for (int num : nums) {
			if (!MapCount.containsKey(num)) {
				MapCount.put(num, 1);
			} else {
				MapCount.put(num, MapCount.get(num) + 1);
			}
		}

		// 2、遍历map，提取最大的count对应的key
		Integer count = null;
		Integer key = null;
		for (Map.Entry<Integer, Integer> entry : MapCount.entrySet()) {
			if (count == null || entry.getValue() > count) {
				count = entry.getValue();
				key = entry.getKey();
			}
		}

		return key;
	}

	public static void main(String[] args) {
		Solution_6 solution_6 = new Solution_6();
		solution_6.majorityElement(new int[]{3,2,3});
	}

}